package com.ve.clexer;

import com.ve.view.editor.span.SpanType;
import com.ve.view.editor.span.TextSpanData;
import com.ve.view.editor.span.TextSpanner;

public class LexerSpannerAdapter implements TextSpanner {
    private TextSpanData spanData;

    public LexerSpannerAdapter() {
    }

    @Override
    public void startSpan(CharSequence content) {
        spanData = new TextSpanData();
        WordBuffer wordBuffer = new WordBuffer();
        int index = 0;
        int row = 1;
        char c;
        for (index = 0; index < content.length(); ++index) {

            c = content.charAt(index);

            if (c == CLanguage.NEWLINE) {
                row++;
            }
            Word word = wordBuffer.analyse(index, row, c);
            if (word != null) {
                addWord(word);
            }
        }

    }

    private void addWord(Word word) {
        SpanType type = SpanType.NORMAL;
        if (word.is(WordType.CHARACTER)) {
            type = SpanType.LITERAL;
        }
        if (word.is(WordType.IDENTIFIER)) {
            if (CLanguage.isKeyWord(word.getData())) {
                type = SpanType.KEYWORD;
            }else if (CLanguage.isBasicTypeWord(word.getData())){
                type = SpanType.SINGLE_SYMBOL_WORD;
            }else {

            }
        }
        if (word.is(WordType.CHARACTER)) {
            type = SpanType.NAME;
        }

        if (word.is(WordType.STRING)) {
            type = SpanType.SINGLE_SYMBOL_DELIMITED;
        }

        if (word.is(WordType.OPERATOR)) {
            type = SpanType.OPERATOR;
        }

        if (word.is(WordType.NUMBER)) {
            type = SpanType.SINGLE_SYMBOL_LINE;
        }
        if (word.is(WordType.ANNOTATION_AREA)) {
            type = SpanType.DOUBLE_SYMBOL_DELIMITED_MULTILINE;
        }

        spanData.addSpan(word.length(), type);
    }

    @Override
    public TextSpanData getSpanData() {
        return spanData;
    }

    @Override
    public void stopSpan() {

    }
}
